package com.fs.erp.api.basedata.controller;

import com.fs.erp.api.basedata.bo.product.brand.QueryProductBrandBo;
import com.fs.erp.api.basedata.entity.ProductBrand;
import com.fs.erp.api.basedata.service.ProductBrandService;
import com.fs.erp.api.basedata.vo.product.brank.QueryProductBrandVo;
import com.fs.erp.common.utils.CollectionUtil;
import com.fs.erp.mybatis.resp.PageResult;
import com.fs.erp.mybatis.utils.PageResultUtil;
import com.fs.erp.web.controller.DefaultBaseController;
import com.fs.erp.web.resp.InvokeResult;
import com.fs.erp.web.resp.InvokeResultBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 商品品牌
 */
@Validated
@RestController
@RequestMapping("/basedata/product/brand")
public class ProductBrandController extends DefaultBaseController {
    @Autowired
    private ProductBrandService brandService;

    /**
     * 品牌列表
     * @param vo
     * @return
     */
    @GetMapping("/query")
    public InvokeResult<PageResult<QueryProductBrandBo>> queryBrand(@Valid QueryProductBrandVo vo){
        PageResult<ProductBrand> pageResult=brandService.query(getPageIndex(vo),getPageSize(vo),vo);
        List<ProductBrand> brands=pageResult.getDatas();
        List<QueryProductBrandBo> resultBrand=null;
        if (!CollectionUtil.isEmpty(brands)){
            resultBrand = brands.stream().map(QueryProductBrandBo::new).collect(Collectors.toList());
        }
        return InvokeResultBuilder.success(PageResultUtil.rebuild(pageResult,resultBrand));
    }
}
